<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
    <style>
        #shop-app .notAdmin::after {
            border: 0 !important;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="shop-app">
    <div class="container">
        <div class="tableBar">
            <el-input
                    v-model="input"
                    placeholder="请输入店名"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>
            <el-button
                    type="primary"
                    @click="addShopHandle('add')"
            >
                + 添加店铺
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
        >
            <el-table-column
                    prop="id"
                    label="店铺ID"
            ></el-table-column>
            <el-table-column
                    prop="name"
                    label="店名"
            ></el-table-column>
            <el-table-column
                    prop="lng"
                    label="经度"
            ></el-table-column>
            <el-table-column
                    prop="lat"
                    label="纬度"
            ></el-table-column>
            <el-table-column
                    prop="locationName"
                    label="地址简称"
            ></el-table-column>

            <el-table-column
                    prop="provinceName"
                    label="省份"
            ></el-table-column>

            <el-table-column
                    prop="cityName"
                    label="城市"
            ></el-table-column>

            <el-table-column
                    prop="districtName"
                    label="区域"
            ></el-table-column>

            <el-table-column
                    prop="detail"
                    label="详细地址"
            ></el-table-column>
            <el-table-column
                    prop="phone"
                    label="手机号"
            ></el-table-column>

            <el-table-column
                    prop="contactName"
                    label="联系人"
            ></el-table-column>

            <el-table-column label="状态">
                <template slot-scope="scope">
                    {{ getStatusText(scope.row.status) }}
                </template>
            </el-table-column>

            <el-table-column
                    label="操作"
                    width="160"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="addShopHandle(scope.row.id)"
                            :class="{notAdmin:user !== 'admin'}"
                    >
                        编辑
                    </el-button>
                    <el-select v-model="scope.row.status" size="small" v-if="user === 'admin'"
                               @change="statusHandle(scope.row)">
                        <el-option label="准备营业" value="0"></el-option>
                        <el-option label="正常营业" value="1"></el-option>
                        <el-option label="暂停营业" value="2"></el-option>
                        <el-option label="已关闭" value="3"></el-option>
                    </el-select>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/shop.js"></script>
<script>
    new Vue({
        el: '#shop-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                id: '',
            }
        },
        computed: {},
        created() {
            this.init()
            if (localStorage.getItem('userInfo') != null) {
                //获取当前登录员工的账号，并赋值给模型数据user
                this.user = JSON.parse(localStorage.getItem('userInfo')).username
            }
        },
        mounted() {
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined
                }
                await getShopList(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加
            addShopHandle(st) {
                if (st === 'add') {
                    window.parent.menuHandle({
                        id: '1',
                        url: '/backend/page/shop/add.html',
                        name: '添加店铺'
                    }, true)
                } else {
                    window.parent.menuHandle({
                        id: '1',
                        url: '/backend/page/shop/add.html?id=' + st,
                        name: '修改店铺'
                    }, true)
                }
            },
            //状态修改
            statusHandle(row) {
                this.id = row.id
                this.status = row.status
                this.$confirm('确认调整该店铺的状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    changeShopStatus({'id': this.id, 'status': this.status}).then(res => {
                        console.log('changeShopStatus', res)
                        if (String(res.code) === '1') {
                            this.$message.success('店铺状态更改成功！')
                            this.handleQuery()
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            getStatusText(status) {
                switch (status) {
                    case 0:
                        return '准备营业';
                    case 1:
                        return '正常营业';
                    case 2:
                        return '暂停营业';
                    case 3:
                        return '已关闭';
                    default:
                        return '未知状态';
                }
            }
        }
    })
</script>
</body>
</html>
